const express = require('express');
const router = express.Router();
const { query } = require('../models/db');

router.post('/login', async (req, res) => {
	const { username, password } = req.body;
	try {
		const sql = 'SELECT * FROM mgtv.users WHERE username=? AND password=?';
		const results = await query(sql, [username, password]);
		if (results.length > 0) {
			const user = results[0];
			delete user.password;
			res.json({
				success: true,
				code: 200,
				data: user,
			});
		} else {
			res.json({
				success: false,
				code: 404,
				message: '用户名或密码错误',
			});
		}
	} catch {
		res.json({
			success: false,
			code: 404,
			message: '服务器错误，请稍后重试',
		});
	}
});

module.exports = router;
